package client;

import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL13;
import org.lwjgl.opengl.GL20;
import org.lwjgl.util.vector.Matrix4f;

import shared.Button;
import shared.MouseHook;
import shared.MyListener;
import shared.Portrait;
import shared.Scene;
import shared.Tools;
import shared.Vec2f;
import shared.Window;

public class Player_Scene implements Scene, MyListener{

	Matrix4f m_viewmatrix;
	FloatBuffer matrix44Buffer;
	int [] m_variables;
	int []m_textureIDs;
	Portrait m_portrait;
	Window m_window;
	public Player_Scene(int [] variables)
	{
	GL11.glClearColor(0.4f, 0.6f, 0.9f, 0f);		
		
		m_variables=variables;
		m_textureIDs=new int[5];
		m_textureIDs[0]=Tools.loadPNGTexture("assets/art/window.png", GL13.GL_TEXTURE0);
		m_textureIDs[1]=Tools.loadPNGTexture("assets/art/button.png", GL13.GL_TEXTURE0);		
		m_textureIDs[2]=Tools.loadPNGTexture("assets/art/font.png", GL13.GL_TEXTURE0);		
		m_textureIDs[3]=Tools.loadPNGTexture("assets/art/grey.png", GL13.GL_TEXTURE0);	
		m_textureIDs[4]=Tools.loadPNGTexture("assets/art/spritesheet.png", GL13.GL_TEXTURE0);	
		//m_buttons=new Button[2];
		m_window=new Window(new Vec2f(-10,-8.5F),new Vec2f(20,9),m_textureIDs[0],true);

		m_viewmatrix=new Matrix4f();
		m_viewmatrix.m00=0.08F;
		m_viewmatrix.m11=0.1F;
		m_viewmatrix.m22=1.0F;
		m_viewmatrix.m33=1.0F;	
		


		m_portrait=new Portrait(new Vec2f(14,0.5F),m_variables[0],m_textureIDs[4],m_textureIDs[1]);
	
		m_window.Add(m_portrait);
		matrix44Buffer=BufferUtils.createFloatBuffer(16);		
		
		//build 8 buttons for customization
		Button [] buttons=new Button[8];
		buttons[0]=new Button(new Vec2f(0.5F,0.5F), new Vec2f(5,2),m_textureIDs, 1, this, "Skin+", 2);
		buttons[1]=new Button(new Vec2f(5.5F,0.5F), new Vec2f(5,2),m_textureIDs, 1, this, "Skin-", 3);		
		buttons[2]=new Button(new Vec2f(0.5F,2.5F), new Vec2f(5,2),m_textureIDs, 1, this, "Gender+", 4);		
		buttons[3]=new Button(new Vec2f(5.5F,2.5F), new Vec2f(5,2),m_textureIDs, 1, this, "Gender-", 5);
		buttons[4]=new Button(new Vec2f(0.5F,4.5F), new Vec2f(5,2),m_textureIDs, 1, this, "Hair+", 6);
		buttons[5]=new Button(new Vec2f(5.5F,4.5F), new Vec2f(5,2),m_textureIDs, 1, this, "Hair-", 7);	
		buttons[6]=new Button(new Vec2f(0.5F,6.5F), new Vec2f(5,2),m_textureIDs, 1, this, "Style+", 8);
		buttons[7]=new Button(new Vec2f(5.5F,6.5F), new Vec2f(5,2),m_textureIDs, 1, this, "Style-", 9);	
		
		for (int i=0;i<8;i++)
		{
			m_window.Add(buttons[i]);
		}
		
		
	}
	
	@Override
	public void Update(float dt) {
		// TODO Auto-generated method stub
		m_window.Update();	
	}

	@Override
	public void Draw() 
	{

		GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);

		//need to set use shader here
		//GL20.glUseProgram(m_pshader);
		GL13.glActiveTexture(GL13.GL_TEXTURE0);
		
		//set tint to default
		GL20.glUniform4f(m_variables[0],1.0F,1.0F,1.0F, 1);
		
		//set standard viewing matrix
		m_viewmatrix.store(matrix44Buffer); matrix44Buffer.flip();
		GL20.glUniformMatrix4(m_variables[1], false, matrix44Buffer);	
		m_window.Draw(matrix44Buffer, m_variables[2]);
		
	}

	@Override
	public void Start(MouseHook mouse) {
		// TODO Auto-generated method stub
		mouse.Register(m_window);

	}

	@Override
	public void End() {
		// TODO Auto-generated method stub
		m_window.Discard();
	}

	public void ButtonCallback(int ID, Vec2f p) 
	{
		switch (ID)
		{
		case 2:
			m_portrait.DecrementSkinTone();
			break;
		case 3:
			m_portrait.IncrementSkinTone();
			break;
		case 4:
			m_portrait.IncrementGender();
			break;
		case 5:
			m_portrait.DecrementGender();
			break;	
		case 6:
			m_portrait.IncrementHairColour();
			break;
		case 7:
			m_portrait.DecrementHairColour();
			break;		
		case 8:
			m_portrait.DecrementHair();
			break;
		case 9:
			m_portrait.IncrementHair();
			break;		
		}
		
	}
	
}
